package ui.troopmanager.mooresr.us;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Person;

import com.toedter.calendar.JDateChooser;

public class TrainingDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private HashMap<Integer, JLabel> labelMap;
	private HashMap<Integer, JDateChooser> dateChooserMap;
	@SuppressWarnings("unused")
	private JPanel centerPanel;
	@SuppressWarnings("unused")
	private JPanel buttonPanel;
	@SuppressWarnings("unused")
	private JButton okButton;
	@SuppressWarnings("unused")
	private JButton cancelButton;
	
	private Integer personId;
	private HashMap<Integer, String> trainingTypes;
	
	
	public TrainingDialog() {
		initComponents();
	}

	private void initComponents() {
		trainingTypes = DataManager.getInstance().getAllTrainingTypes();
		labelMap = new HashMap<Integer, JLabel>();
		dateChooserMap = new HashMap<Integer, JDateChooser>();
		
		JPanel centerPanel = new JPanel();
		JPanel buttonPanel = new JPanel();
		JButton okButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");
		
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		okButton.addActionListener(new ActionListener() {	
			public void actionPerformed(ActionEvent event) {
				okButtonCallback(event);
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {	
			public void actionPerformed(ActionEvent event) {
				cancelButtonCallback(event);
			}
		});
		        
		centerPanel.setLayout(new GridLayout(0, 2));
		
		Font headerFont = new Font(Font.DIALOG, Font.BOLD, 14);
		JLabel header1 = new JLabel("Training");
		header1.setFont(headerFont);
		JLabel header2 = new JLabel("Date Taken");
		header2.setFont(headerFont);
		
		centerPanel.add(header1);
		centerPanel.add(header2);
		
		for (Integer id : trainingTypes.keySet()) {
			JLabel l = new JLabel(trainingTypes.get(id));
			JDateChooser d =  new JDateChooser(); 
			
			labelMap.put(id, l);
			dateChooserMap.put(id, d);
			
			centerPanel.add(l);
			centerPanel.add(d);
			
		}
		
		setLayout(new BorderLayout());
		add(centerPanel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
		
		
		setSize(600, 400);
		setModal(true);
	}

	public void loadData(Person p) {
		setTitle(p.toString() + " - Training Record");
		
		HashMap<Integer, Date> trainingRecord = DataManager.getInstance().getTrainingRecord(p);
		
		// Clear the date choosers first
		for (JDateChooser control : dateChooserMap.values()) {
			control.setDate(null);
		}
		
		for (Integer id : trainingRecord.keySet()) {
			JDateChooser control = dateChooserMap.get(id);
			if (control != null) {
				control.setDate(trainingRecord.get(id));
			}
		}
		
		personId = p.getPersonId();
	}
	
	public void okButtonCallback(ActionEvent event) {
	
		HashMap<Integer, Date> trainingRecord = new HashMap<Integer, Date>();
		
		for (Integer key : dateChooserMap.keySet()) {
			java.util.Date controlValue = dateChooserMap.get(key).getDate();
			if (controlValue != null) {
				Date d = new Date(controlValue.getTime());
				trainingRecord.put(key, d);
			}
		}
		
		DataManager.getInstance().setTrainingRecord(personId, trainingRecord);
		
		setVisible(false);
	}
	
	public void cancelButtonCallback(ActionEvent event) {
		setVisible(false);
	}
	
}
